<?php

class m111006_074500_account_token extends CDbMigration {
	public function safeUp() {
		$this
			->createTable('erp_account_token_category',
				array(
					'id' => 'pk',
					'parentId' => 'int',
					'name' => 'string',
					'createTime' => 'datetime',
					'createUserId' => 'int',
					'updateTime' => 'datetime',
					'updateUserId' => 'int',
				));
		$this
			->createIndex("account_token_category_create_user_id", 'erp_account_token_category',
				'createUserId');
		$this
			->addForeignKey('fk_account_token_category_create_user_id',
				'erp_account_token_category', 'createUserId', 'erp_user', 'id', 'SET NULL',
				'CASCADE');
		$this
			->createIndex("account_token_category_update_user_id", 'erp_account_token_category',
				'updateUserId');
		$this
			->addForeignKey('fk_account_token_category_update_user_id',
				'erp_account_token_category', 'updateUserId', 'erp_user', 'id', 'SET NULL',
				'CASCADE');

		$this
			->createIndex("account_token_category_parent_id", 'erp_account_token_category',
				'parentId');
		$this
			->addForeignKey('fk_account_token_category_parent_id', 'erp_account_token_category',
				'parentId', 'erp_account_token_category', 'id', 'CASCADE', 'CASCADE');

		$this
			->createTable('erp_account_token',
				array(
					'id' => 'pk',
					'tokenDate' => 'date',
					'tokenYear' => 'int',
					'tokenMonth' => 'int',
					'number' => 'int',
					'note' => 'string',
					'createTime' => 'datetime',
					'createUserId' => 'int',
					'updateTime' => 'datetime',
					'updateUserId' => 'int',
				));
		$this->createIndex("account_token_create_user_id", 'erp_account_token', 'createUserId');
		$this
			->addForeignKey('fk_account_token_create_user_id', 'erp_account_token', 'createUserId',
				'erp_user', 'id', 'SET NULL', 'CASCADE');
		$this->createIndex("account_token_update_user_id", 'erp_account_token', 'updateUserId');
		$this
			->addForeignKey('fk_account_token_update_user_id', 'erp_account_token', 'updateUserId',
				'erp_user', 'id', 'SET NULL', 'CASCADE');

		$this
			->createTable('erp_account_token_item',
				array(
					'id' => 'pk',
					'tokenId' => 'int',
					'categoryId' => 'int',
					'summary' => 'string',
					'debit' => 'decimal(16,2)',
					'credit' => 'decimal(16,2)',
					'createTime' => 'datetime',
					'createUserId' => 'int',
					'updateTime' => 'datetime',
					'updateUserId' => 'int',
				));

		$this
			->createIndex("account_token_item_create_user_id", 'erp_account_token_item',
				'createUserId');
		$this
			->addForeignKey('fk_account_token_item_create_user_id', 'erp_account_token_item',
				'createUserId', 'erp_user', 'id', 'SET NULL', 'CASCADE');
		$this
			->createIndex("account_token_item_update_user_id", 'erp_account_token_item',
				'updateUserId');
		$this
			->addForeignKey('fk_account_token_item_update_user_id', 'erp_account_token_item',
				'updateUserId', 'erp_user', 'id', 'SET NULL', 'CASCADE');

		$this
			->createIndex("account_token_item_category_id", 'erp_account_token_item', 'categoryId');
		$this
			->addForeignKey('fk_account_token_item_category_id', 'erp_account_token_item',
				'categoryId', 'erp_account_token_category', 'id', 'CASCADE', 'CASCADE');
		$this->createIndex("account_token_item_token_id", 'erp_account_token_item', 'tokenId');
		$this
			->addForeignKey('fk_account_token_item_token_id', 'erp_account_token_item', 'tokenId',
				'erp_account_token', 'id', 'CASCADE', 'CASCADE');
	}

	public function safeDown() {
		$this->dropTable('erp_account_token_item');
		$this->dropTable('erp_account_token');
		$this->dropTable('erp_account_token_category');
	}
}
